$(function () {
    var role_datagrid = $("#role_datagrid");
    var role_dialog = $("#role_dialog");
    var role_form = $("#role_form");
    var allPermissions = $("#allPermissions");
    var selfPermissions = $("#selfPermissions");

    //缓存系统权限数据
    var allData = {};
    allPermissions.datagrid({
        width: 250,
        height: 300,
        url: "permission/queryAll",
        title: "系统权限",
        fitColumns: true,
        striped: true,
        singleSelect: true,
        rownumbers: true,
        columns: [[
            {field: "id", hidden: true},
            {field: "name", title: "权限名称", width: 100, align: "center"},

        ]],
        onDblClickRow: function (index, row) {
            allPermissions.datagrid("deleteRow", index);
            selfPermissions.datagrid("appendRow", row);
        },
        onLoadSuccess: function (data) {
            //此方式是有问题,
            //原因: data直接是地址赋值, 修改时, allData也会发生, 应该采用拷贝方式
            //allData = data;//将加载数据缓存起来
            allData = $.extend(true, {}, data);
            //console.log(allData);
        }
    });
    //自己拥有的权限
    selfPermissions.datagrid({
        width: 250,
        height: 300,
        title: "自身权限",
        fitColumns: true,
        striped: true,
        singleSelect: true,
        rownumbers: true,
        columns: [[
            {field: "id", hidden: true},
            {field: "name", title: "权限名称", width: 100, align: "center"},

        ]],
        onClickRow: function (index, row) {
            selfPermissions.datagrid("deleteRow", index);
            allPermissions.datagrid("appendRow", row);
        },
        //去重
        onLoadSuccess: function (data) {
            //找到自身权限集合
            var ids = $.map(data.rows, function (item) {
                return item.id;
            });
            //查找系统权限集合
            var rows = allPermissions.datagrid("getRows");
            for (var i = rows.length - 1; i >= 0; i--) {
                if ($.inArray(rows[i].id, ids) >= 0) {
                    allPermissions.datagrid("deleteRow", i);
                }
            }
        }
    });

    var cmdObj = {
        //工具栏的刷新
        reload: function () {
            role_datagrid.datagrid("reload");
        },
        //工具栏添加
        add: function () {
            //1.弹框
            role_dialog.dialog("open");
            //设置标题
            role_dialog.dialog("setTitle", "角色添加");
            //添加与编辑时
            role_form.form("clear");
            //清空自身权限
            selfPermissions.datagrid("loadData", {"total": 0, "rows": []});
            //从缓存中加载系统权限
            allPermissions.datagrid("loadData", allData);
        },
        //保存操作
        save: function () {
            var id = $("#roleId").val();
            var url = "/role/save";
            if (id) {
                url = "/role/update";
            }
            $("#role_form").form('submit', {
                url: url,
                //提交表单前封装额外数据
                onSubmit: function (param) {
                    var rows = selfPermissions.datagrid("getRows");
                    for (var i = 0; i < rows.length; i++) {
                        param["permissions[" + i + "].id"] = rows[i].id;
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);

                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        //关闭框
                        $("#role_dialog").dialog("close");
                        //刷新列表数据
                        $("#role_datagrid").datagrid("reload");
                    }
                }
            })
        },
        //关闭
        cancel: function () {
            $("#role_dialog").dialog("close");
        },

        //编辑
        edit: function () {
            //判断是否选中数据
            var row = role_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据", "info");
                return;
            }
            //1.弹框
            role_dialog.dialog("open");
            //设置标题
            role_dialog.dialog("setTitle", "角色编辑");
            //添加与编辑时将数据清空
            role_form.form("clear");

            //数据回显: 同名原则
            role_form.form("load", row);
            //从缓存中加载系统权限
            allPermissions.datagrid("loadData", allData);

            //加载当前编辑角色具有的权限
            //设置url
            var options = selfPermissions.datagrid("options");
            options.url = "/permission/queryPermissionByRoleId?roleId=" + row.id;
            //重新加载数据
            selfPermissions.datagrid("load");


        },
        //角色删除
        remove: function () {
            //判断是否选中数据
            var row = role_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要编辑的数据");
                return;
            }
            $.messager.confirm('确认', '您确认想要执行这个操作吗？', function (r) {
                if (r) {
                    $.get("/role/delete", {id: row.id}, function (data) {
                        if (data.success) {
                            //刷新列表数据
                            role_datagrid.datagrid("reload");
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    })
                }
            });
        }

    };

    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

    role_datagrid.datagrid({
        url: "role/list",
        fit: true,
        fitColumns: true,
        striped: true,
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        toolbar: '#tb',
        columns: [[
            {field: "ck", checkbox: true},//显示多选框样式
            {field: "id", hidden: true},
            {field: "sn", title: "编号", width: 100},
            {field: "name", title: "名称", width: 100}

        ]]
    });

    role_dialog.dialog({
        width: 600,
        height: 500,
        buttons: "#bb",
        closed: true
    })


});






